﻿using System;
using System.Diagnostics;
using System.Management;
using System.Net;
using System.Runtime.InteropServices;
using System.Net.Sockets;//新加的

namespace RxjhServer
{
	public class Hasher
	{
		[DllImport("kernel32")]
		public static extern void GetSystemInfo(ref Hasher.CPU_INFO cpuinfo);

		public static string GetCpuID()
		{
			string result;
			try
			{
				ManagementClass managementClass = new ManagementClass("Win32_Processor");
				ManagementObjectCollection instances = managementClass.GetInstances();
				string text = null;
				using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = instances.GetEnumerator())
				{
					if (enumerator.MoveNext())
					{
						ManagementObject managementObject = (ManagementObject)enumerator.Current;
						text = managementObject.Properties["ProcessorId"].Value.ToString();
					}
				}
				result = text;
			}
			catch (Exception)
			{
				result = "CpUIDerror";
			}
			return result;
		}

        public static String GetIP()
        {
            //IPHostEntry hostip = Dns.GetHostEntry(Dns.GetHostName());
            //IPAddress[] addrList = hostip.AddressList;
            //return addrList[0].ToString();
            IPHostEntry hostip = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress[] addrList = hostip.AddressList;
            for (int i = 0; i < addrList.Length; i++)
            {
                //toolStripComboBox1.Items.Add(addrList[i].ToString());
                if (addrList[i].AddressFamily == AddressFamily.InterNetwork)
                {
                    return addrList[i].ToString();
                    //  strSelectItem = toolStripComboBox1.Items[i].ToString();
                }
            }
            return addrList[0].ToString();
        }

        public static string GetMac()
		{
			string result = string.Empty;
			try
			{
				ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
				ManagementObjectCollection instances = managementClass.GetInstances();
				using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = instances.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ManagementObject managementObject = (ManagementObject)enumerator.Current;
						if (managementObject["IPEnabled"].ToString() == "True")
						{
							result = managementObject["MacAddress"].ToString();
						}
					}
				}
			}
			catch
			{
				result = string.Empty;
			}
			return result;
		}

		[DllImport("kernel32.dll")]
		private static extern int GetVolumeInformation(string lpRootPathName, string lpVolumeNameBuffer, int nVolumeNameSize, ref int lpVolumeSerialNumber, int lpMaximumComponentLength, int lpFileSystemFlags, string lpFileSystemNameBuffer, int nFileSystemNameSize);

        public static string GetDriveID(string drvID)
        {

            try

            {

                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

                String strHardDiskID = null;

                foreach (ManagementObject mo in searcher.Get())

                {
                    strHardDiskID = mo["SerialNumber"].ToString().Trim();

                    break;

                }

                return strHardDiskID;

            }

            catch

            {

                return "0";

            }
            // GetHd();
            //const int MAX_FILENAME_LEN = 256;
            //int retVal = 0;
            //int a = 0;
            //int b = 0;
            //string str1 = null;
            //string str2 = null;
            //int i = GetVolumeInformation(
            // drvID + @":\",
            // str1,
            // MAX_FILENAME_LEN,
            // ref retVal,
            // a,
            // b,
            // str2,
            // MAX_FILENAME_LEN
            // );
            //return retVal.ToString("x");
            //try
            //{
            //    string strSN = AtapiDevicea.getHad();
            //    //ManagementObject m_objDisk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
            //    //ManagementObject数据管理对象   
            //    //string strSN = (string)m_objDisk.GetPropertyValue("VolumeSerialNumber");
            //    //获得获取某属性值的等效访问器   
            //    return strSN;
            //}
            //catch (Exception)
            //{
            //    return "0";
            //}
        }

        public static string cmd(string cmd)
		{
			string result = string.Empty;
			try
			{
				Process process = new Process();
				process.StartInfo.FileName = "cmd.exe";
				process.StartInfo.UseShellExecute = false;
				process.StartInfo.RedirectStandardInput = true;
				process.StartInfo.RedirectStandardOutput = true;
				process.StartInfo.CreateNoWindow = true;
				process.Start();
				process.StandardInput.WriteLine(cmd);
				process.StandardInput.WriteLine("exit");
				result = process.StandardOutput.ReadToEnd();
				process.Close();
			}
			catch (Exception ex)
			{
				result = "执行DOS命令错误" + ex.Message.ToString();
			}
			return result;
		}

		public struct CPU_INFO
		{
			public uint dwOemId;

			public uint dwPageSize;

			public uint lpMinimumApplicationAddress;

			public uint lpMaximumApplicationAddress;

			public uint dwActiveProcessorMask;

			public uint dwNumberOfProcessors;

			public uint dwProcessorType;

			public uint dwAllocationGranularity;

			public uint dwProcessorLevel;

			public uint dwProcessorRevision;
		}
	}
}
